part of pda.service;

class CheckBill implements SqlOperation {
  static const String tableName = 'pda_check';

//  id             VARCHAR (40)  NOT NULL,
  String id;

//  check_date     TIMESTAMP     NOT NULL,-- 盘点日期
  DateTime checkDate;

//  depart         VARCHAR (10)  NOT NULL,-- 盘点仓库
  String depart;

//  device_id      VARCHAR (50),-- android设备id
  String devicedId;

//  user_device_id VARCHAR (30),-- 用户自定义设备id
  String userDevicedId;

//  sn             VARCHAR (30),-- 单号
  String sn;

//  upload_flag    INTEGER       DEFAULT 0,-- 数据是否已上传
  int uploadFlag;

//  creator        varchar(10),
  String creator;

//  create_time    TIMESTAMP     DEFAULT (datetime('now', 'localtime') ),
  DateTime createTime;

//  need_audit     INTEGER,-- 是否需要在服务端审核
  int needAudit;

//  printer_id     INTEGER,-- 选择的打印机
  int printerId;

//  memo           VARCHAR (100),-- 备注
  String memo;

  CheckBill(
      {this.id,
      this.checkDate,
      this.depart,
      this.devicedId,
      this.userDevicedId,
      this.sn,
      this.uploadFlag,
      this.creator,
      this.createTime,
      this.needAudit,
      this.printerId,
      this.memo});

  factory CheckBill.fromSqlMap(Map<String, dynamic> map) {
    return fromMap(map);
  }

  static CheckBill fromMap(Map<String, dynamic> map) {
    return CheckBill(
      id: map['id'],
      checkDate: map['check_date'],
      depart: map['depart'],
      devicedId: map['device_id'],
      userDevicedId: map['user_device_id'],
      sn: map['sn'],
      uploadFlag: map['upload_flag'],
      creator: map['creator'],
      createTime: map['create_time'],
      needAudit: map['need_audit'],
      printerId: map['printer_id'],
      memo: map['memo'],
    );
  }

  Future<List> loadItems() async {
    return SqliteCRUD.query(
        CheckBillItem.tableName, CheckBillItem.fromMap, 'check_id = ?', [id]);
  }

  Map<String, dynamic> toSqlMap() {
    return {
      'id': id,
      'check_date': checkDate,
      'depart': depart,
      'device_id': devicedId,
      'user_device_id': userDevicedId,
      'sn': sn,
      'upload_flag': uploadFlag,
      'creator': creator,
      'create_time': createTime,
      'need_audit': needAudit,
      'printer_id': printerId,
      'memo': memo,
    };
  }

  @override
  Map<String, dynamic> updateParameters() {
    return toSqlMap();
  }

  @override
  bool beforeUpdate() {
    return id != null && uploadFlag == 0;
  }

  @override
  Map<String, dynamic> insertParameters() {
    return toSqlMap();
  }

  @override
  String pkValue() {
    return id;
  }

  @override
  String getTableName() {
    return tableName;
  }
}

class CheckBillItem {
  static const String tableName = 'pda_check_item';

//  id          VARCHAR (40)    NOT NULL,
  String id;

//  code        VARCHAR (20)    NOT NULL,-- 商品的主键
  String code;

//  name        VARCHAR (50),
  String name;

//  price       DECIMAL (10, 4),
  double price;

//  qty         DECIMAL (10, 2),
  double qty;

//  barcode     VARCHAR (20),
  String barcode;

//  spec        VARCHAR (30),
  String spec;

//  remark      VARCHAR (50),
  String remark;

//  check_id    VARCHAR (40)    NOT NULL,-- 关联的主表id
  String checkId;

//  create_time TIMESTAMP       DEFAULT (datetime('now', 'localtime') ),
  DateTime createTime;

  CheckBillItem(this.id, this.checkId,
      {this.code,
      this.name,
      this.price,
      this.qty,
      this.barcode,
      this.spec,
      this.remark,
      this.createTime});

  static CheckBillItem fromMap(Map<String, dynamic> map) {
    return CheckBillItem(map['id'], map['check_id'],
        code: map['code'],
        name: map['name'],
        price: map['price'],
        qty: map['qty'],
        barcode: map['barcode'],
        spec: map['spec'],
        remark: map['remark'],
        createTime: map['create_time']);
  }
}
